/*
 * @Date: 2023-03-23 15:11:16
 * @LastEditTime: 2023-05-15 17:01:50

 * 介绍:工具函数
 */

import { queryToObj } from "./object";
/**
 * * 获取资源路径
 * @param path 从src开始键入
 * @returns
 */
export function _require(path: string) {
  const imagesUrl = import.meta.env.VITE_IMAGES_URL;
  const useImagesUrl = import.meta.env.VITE_USE_IMAGES_URL == "1";
  const imagesRegExp = /^src\/assets\/images/;
  if (useImagesUrl && imagesRegExp.test(path))
    return path.replace(imagesRegExp, imagesUrl);

  return new URL(`../../${path.replace(/^src\//, "")}`, import.meta.url).href;
}

/**获取当前地址的query参数 */
export function getCurrentHrefQueryParam() {
  const url = window.location.href;
  const urlArr = url.split("?");
  urlArr.shift();
  const obj: { [key: string]: any } = {};
  urlArr.forEach((val) => {
    Object.assign(obj, queryToObj("?" + val));
  });
  return obj;
}
